    import { Navigate } from "react-router-dom"
    import {useSelector,useDispatch} from "react-redux"
    import { useEffect, useState } from "react";
    import { check_logins } from "../service/login";
    import { setUser } from "../store/modules/login";
import LoadingPage from "./loading/LoadingPage";
    export const AuthRouter=({children})=>{
        const {token}=useSelector(d=>d.login);
        const dispatch=useDispatch();
        const [isCheck,set_isCheck]=useState(null);
       
        
        //检查登录
        useEffect(()=>{
            const check_login=async()=>{
                    const res=await check_logins();
                    if(res.code==0){
                        dispatch(setUser(res.data))
                        setTimeout(() => {
                            set_isCheck(true);
                      
                        }, 1400);
                        
                    }else{
                        set_isCheck(false);
                    }
            }

            check_login();
        },[])
        
        if(isCheck==null){
            return <>
            <LoadingPage></LoadingPage>
            </>
        }
         if(!token || !isCheck){
            return <Navigate to={"/login"}></Navigate>
        }


        return <>
        {children}
        </>
    }